package cn.woblog.android.http;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.woblog.android.common.utils.Const;
import cn.woblog.android.common.utils.ToastAlone;
import cn.woblog.android.http.cache.CacheControl;
import cn.woblog.android.http.callback.ICallback;
import cn.woblog.android.http.request.EncodeForm;

@SuppressLint("NewApi")
public class Http {
	private final static OkHttpClient client = new OkHttpClient();
	static {
		client.setConnectTimeout(10, TimeUnit.SECONDS);
		client.setWriteTimeout(10, TimeUnit.SECONDS);
		client.setReadTimeout(30, TimeUnit.SECONDS);
	}
	private static Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Const.MSG_SHOW_OFFLINE_INFO:
				showOfflineToast();
				break;

			default:
				break;
			}
		};
	};
	private static Map<String, Call> requestCaches = new HashMap<String, Call>();
	private static CacheControl cacheControl = new CacheControl();
	private static Context context;

	public static Context getContext() {
		return context;
	}

	protected static void showOfflineToast() {
		ToastAlone.showToast((Activity) context, "当前网络错误,只能使用离线", 0);
	}

	public static void setContext(Context context) {
		Http.context = context;
	}

	public static CacheControl getCacheControl() {
		return cacheControl;
	}

	public static void setCacheControl(CacheControl cacheControl) {
		Http.cacheControl = cacheControl;
	}

	public static void addCache(String key, Call value) {
		requestCaches.put(key, value);
	}

	public static void cancel(String tag) {
		Call call = requestCaches.get(tag);
		if (call != null && !call.isCanceled()) {
			call.cancel();
			requestCaches.remove(tag);
		}
	}

	public static void removeCall(String tag) {
		requestCaches.remove(tag);
	}

	public static <T> void post(final String url, final EncodeForm form, final ICallback<T> callback) {
		post(url, form, callback, null);
	}

	public static <T> void post(final String url, final EncodeForm form, final ICallback<T> callback, String tag) {
		Request request = null;
		if (form == null) {
			request = new Request.Builder().url(url).post(null).build();
		} else {
			RequestBody formBody = form.build();
			request = new Request.Builder().url(url).post(formBody).build();
		}
		excuteTask(createRequestTask(callback, tag, request));
	}

	public static <T> void get(final String url, final EncodeForm form, final ICallback<T> callback, String tag) {
		String formUrl = null;
		if (form == null) {
			formUrl = url;
		} else {
			formUrl = form.buildString(url);
		}
		Request request = new Request.Builder().url(formUrl).build();
		excuteTask(createRequestTask(callback, tag, request));
	}

	public static <T> void get(final String url, final EncodeForm form, final ICallback<T> callback) {
		get(url, form, callback, null);
	}

	private static <T> RequestTask<T> createRequestTask(final ICallback<T> callback, String tag, Request request) {
		return new RequestTask<T>(client, request, callback, tag);
	}

	private static void excuteTask(AsyncTask<Void, Integer, Object[]> task) {
		if (Build.VERSION.SDK_INT < 11) {
			task.execute((Void) null);
		} else {
			task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
		}
	}

	public static Handler getHandler() {
		return handler;
	}
}
